<?php
/**
 * 易优CMS
 * ============================================================================
 * 版权所有 2016-2028 海南赞赞网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.eyoucms.com
 * ----------------------------------------------------------------------------
 * 如果商业用途务必到官方购买正版授权, 以免引起不必要的法律纠纷.
 * ============================================================================
 * Author: 小虎哥 <1105415366@qq.com>
 * Date: 2018-4-3
 */

namespace think\template\taglib\api;

use think\Db;

/**
 * 文档评论列表
 */
class TagComment extends Base
{
    public $users_id = 0;

    //初始化
    protected function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 获取订单评论列表
     */
    public function apiCommentList($page = 1, $pagesize = 10, $aid = 0, $parse = [], $users = [])
    {
        $parse['statistics'] = empty($parse['statistics']) ? 'off' : $parse['statistics'];
        $orderway = empty($param['orderway']) ? 'desc' : $param['orderway'];
        $orderby = empty($param['orderby']) ? 'comment_id' : $param['orderby'];
        $order = 'a.' . $orderby . ' ' . $orderway;
        $where['a.is_show'] = 1;
        if (!empty($aid)) {
            $where['a.product_id'] = $aid;
        }

        $type = !empty($parse['type']) ? $parse['type'] : '';
        if ($type == 'new') {
            $order = 'a.add_time desc';
        } else if ($type == 'good') {
            $where['a.total_score'] = ['IN', [4, 5]];
        } else if ($type == 'bad') {
            $where['a.total_score'] = ['IN', [1, 2]];
        } else if ($type == 'img') {
            $where['a.total_score'] = ['NEQ', ''];
        }

        $paginate = array(
            'page' => $page,
        );
        $pages = Db::name('shop_order_comment')
            ->alias('a')
            ->field('a.*,b.head_pic,b.nickname,b.sex,c.data')
            ->join('users b', 'a.users_id = b.users_id', 'left')
            ->join('shop_order_details c', 'a.details_id = c.details_id', 'left')
            ->where($where)
            ->order($order)
            ->paginate($pagesize, false, $paginate);
        $result = $pages->toArray();
        if (!empty($result['data'])) {
            //统计点赞数量
            $comment_ids = get_arr_column($result['data'], 'comment_id');
            $where = [
                'comment_id' => ['IN', $comment_ids]
            ];
            $like_count = Db::name('shop_order_comment_like')->where($where)->group('comment_id')->field('count(*) as count, comment_id')->getAllWithIndex('comment_id');
            //我点赞的
            if (!empty($users['users_id'])) $where['users_id'] = $users['users_id'];
            $my_like =  Db::name('shop_order_comment_like')->where($where)->column('comment_id');
            foreach ($result['data'] as $key => $val) {
                $val['like_count'] = !empty($like_count[$val['comment_id']]) ? $like_count[$val['comment_id']]['count'] :0;
                $val['like'] = in_array($val['comment_id'],$my_like) ? 1 :0;
                $val['content'] = unserialize($val['content']);
                if (!empty($val['upload_img'])) $val['upload_img'] = unserialize($val['upload_img']);
                if (!empty($val['upload_img'])) {
                    $val['upload_img'] = explode(',', $val['upload_img']);
                    foreach ($val['upload_img'] as $k => $v) {
                        $val['upload_img'][$k] = $this->get_default_pic($v);
                    }
                }
                $val['data'] = unserialize($val['data']);
                if (!empty($val['data']['spec_value'])) {
                    $val['data']['spec_value'] = htmlspecialchars_decode($val['data']['spec_value']);
                    $val['data']['spec_value'] = explode('<br/>', $val['data']['spec_value']);
                    $count = count($val['data']['spec_value']);
                    unset($val['data']['spec_value'][$count - 1]);
                }

                $val['head_pic'] = $this->get_head_pic($val['head_pic'], false, $val['sex']);
                $val['add_time'] = $this->time_format($val['add_time']);
                if ('' == $val['nickname'] || null == $val['nickname']){
                    $val['nickname'] = '匿名';
                }
                $result['data'][$key] = $val;
            }
        }

        if ('on' == $parse['statistics']) {
            $where_pub = ['product_id' => $aid, 'is_show' => 1];
            $statistics['total'] = Db::name('shop_order_comment')->where($where_pub)->count();
            $statistics['good'] = Db::name('shop_order_comment')->where($where_pub)->where('total_score', 'in', [4, 5])->count('comment_id');
            $statistics['bad'] = Db::name('shop_order_comment')->where($where_pub)->where('total_score', 'in', [1, 2])->count('comment_id');
            if (empty($statistics['good'])) {
                $statistics['good_percent'] = 100;
            } else {
                $statistics['good_percent'] = ceil(($statistics['good'] / $statistics['total']) * 100);
            }
            $statistics['have_img'] = Db::name('shop_order_comment')->where($where_pub)->where('upload_img', 'neq', '')->count('comment_id');
            $statistics['good'] = $this->handle_count($statistics['good']);
            $statistics['bad'] = $this->handle_count($statistics['bad']);
            $statistics['have_img'] = $this->handle_count($statistics['have_img']);
            $statistics['total'] = $this->handle_count($statistics['total']);
            $result['statistics'] = $statistics;
        }

        return $result;
    }

    private function handle_count($value = 0)
    {
        $value_h = 0;
        if (!empty($value)){
            if ($value > 10 && $value <= 100) {
                $value_h = ceil($value / 10) * 10 . '+';
            } else if ($value > 100 && $value <= 1000) {
                $value_h = ceil($value / 100) * 100 . '+';
            } else if ($value > 1000 && $value <= 10000) {
                $value_h = ceil($value / 1000) * 1000 . '+';
            } else if ($value > 10000){
                $value_h = ceil($value / 10000) . '万+';
            }else{
                $value_h = $value;

            }
        }
        return [$value, $value_h];
    }

    public function apiCommentLike($comment_id = 0, $users = [])
    {
        $count = Db::name('shop_order_comment_like')->where(['users_id' => $users['users_id'], 'comment_id' => $comment_id])->count();
        if (!empty($count)) {
            //取消点赞
            Db::name('shop_order_comment_like')->where(['users_id' => $users['users_id'], 'comment_id' => $comment_id])->delete();
            $ret = ['like' => 0, 'msg' => '取消点赞成功'];
        } else {
            //添加点赞
            $insert = [
                'users_id' => $users['users_id'],
                'comment_id' => $comment_id,
                'add_time' => getTime(),
                'update_time' => getTime(),
            ];
            Db::name('shop_order_comment_like')->insert($insert);
            $ret = ['like' => 1, 'msg' => '点赞成功'];
        }
        $ret['count'] = Db::name('shop_order_comment_like')->where(['comment_id' => $comment_id])->count();
        return $ret;
    }

}

